(function () {
    'use strict';

    angular.module('app')
        .controller('ScheduleJobListCtrl', ScheduleJobListCtrl);

    ScheduleJobListCtrl.$inject = ['$rootScope', '$scope','$state','$dialogs','ScheduleJobService'];
    function ScheduleJobListCtrl($rootScope, $scope,  $state,$dialogs,scheduleJobService) {

		$scope.title = '定时任务';
		$scope.params = {
            pageNum: 1,
            pageSize: 20,
            orderBy: ''
        };


		$scope.search = function () {
			$rootScope.loading = true;
            scheduleJobService.list($scope.params)
                .then(function (data) {
					$scope.pageInfo = data;
                })
                .finally(function () {
					$rootScope.loading = false;
                })
            ;
        };


		$scope.search();

		$scope.pageChanged = function () {
			$scope.params.pageNum = $scope.pageInfo.pageNum;
				$scope.search();
        };


        $scope.delete = function (id) {

            dialogs.confirm('确认', '要删除此定时任务吗？', {
                size: 'md'
            })
                .result.then(function () {
                scheduleJobService.deleteById(id)
                    .then(function () {
                        $state.reload();
                    })
                ;
            });
        };
        $scope.run = function (id) {

            dialogs.confirm('确认', '要立即执行定时任务吗？', {
                size: 'md'
            })
                .result.then(function () {
                scheduleJobService.run(id)
                    .then(function () {
                        $state.reload();
                    })
                ;
            });
        };


        $scope.pause = function (id) {

            dialogs.confirm('确认', '要暂停定时任务吗？', {
                size: 'md'
            })
                .result.then(function () {
                scheduleJobService.pause(id)
                    .then(function () {
                        $state.reload();
                    })
                ;
            });
        };

        $scope.resume = function (id) {

            dialogs.confirm('确认', '要恢复定时任务吗？', {
                size: 'md'
            })
                .result.then(function () {
                scheduleJobService.resume(id)
                    .then(function () {
                        $state.reload();
                    })
                ;
            });
        };
    }
})();